package com.qing.demo.netty.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * @author caozhipeng
 * @date 2025/7/25
 */
public class NettyServer {

    public static void main(String[] args) {
        new NettyServer().bing(7379);
    }

    private void bing(int port){
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true) //广播
                    .option(ChannelOption.SO_RCVBUF, 2048 * 1024)//接收缓冲区大小 2M
                    .option(ChannelOption.SO_SNDBUF, 1024 * 1024)//发送缓冲区大小 1M
                    .handler(new MyChannelInitializer());

            ChannelFuture channelFuture = bootstrap.bind(port).sync();
            System.out.println("服务器启动成功");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            group.shutdownGracefully();
        }
    }
}
